<script type="text/html" template>
    <link rel="stylesheet" href="{{ layui.setter.base }}style/login.css?v={{ layui.admin.v }}-1" media="all">
</script>


<div class="layadmin-user-login layadmin-user-display-show" id="LAY-user-login" style="display: none;">

    <div class="layadmin-user-login-main">
        <div class="layadmin-user-login-box layadmin-user-login-header">
            <h2>NLP and Visualization</h2>
            <p>自然语言处理与可视化平台 后端管理系统</p>
        </div>
        <div class="layadmin-user-login-box layadmin-user-login-body layui-form">
            <div class="layui-form-item">
                <label class="layadmin-user-login-icon layui-icon layui-icon-username"
                       for="username"></label>
                <input type="text" name="username" id="username" lay-verify="required"
                       placeholder="管理员名称"
                       class="layui-input">
            </div>
            <div class="layui-form-item">
                <label class="layadmin-user-login-icon layui-icon layui-icon-password"
                       for="password"></label>
                <input type="password" name="password" id="password" lay-verify="required"
                       placeholder="密码" class="layui-input">
            </div>
            <div class="layui-form-item" style="margin-bottom: 20px;">
                <input type="checkbox" name="remember" lay-skin="primary" title="记住密码">
                <a class="layadmin-user-jump-change layadmin-link" style="margin-top: 7px;" onclick="forget()">忘记密码？</a>
            </div>
            <div class="layui-form-item">
                <button class="layui-btn layui-btn-fluid" lay-submit lay-filter="login-btn">登 入</button>
            </div>
        </div>
    </div>

    <div class="layui-trans layadmin-user-login-footer">

        <p>© 2018.6 <a href="/core/index.jsp" target="_blank">桂林电子科技大学-林晓民</a></p>
        <p>
            <span><a href="/core/index.jsp" target="_blank">平台首页</a></span>
            <span><a href="/docs/index.jsp" target="_blank">在线文档</a></span>
        </p>
    </div>

    <!--<div class="ladmin-user-login-theme">
      <script type="text/html" template>
        <ul>
          <li data-theme=""><img src="{{ layui.setter.base }}style/res/bg-none.jpg"></li>
          <li data-theme="#03152A" style="background-color: #03152A;"></li>
          <li data-theme="#2E241B" style="background-color: #2E241B;"></li>
          <li data-theme="#50314F" style="background-color: #50314F;"></li>
          <li data-theme="#344058" style="background-color: #344058;"></li>
          <li data-theme="#20222A" style="background-color: #20222A;"></li>
        </ul>
      </script>
    </div>-->

</div>
<!--引入 cookie管理-->
<script src="/backend/src/lib/common/jquery.cookie.js"></script>
<script>

    layui.use(['admin', 'form', 'user'], function () {
        var $ = layui.$
            , setter = layui.setter
            , admin = layui.admin
            , form = layui.form
            , router = layui.router()
            , search = router.search;

        form.render();

        //提交
        form.on('submit(login-btn)', function (obj) {

            var obj = new Object();
            obj.admin = $("#username").val();
            obj.password = $("#password").val();
            var requestJson = JSON.stringify(obj);
            //发送异步请求
            jQuery.ajax({
                url: '/admin/login.do',
                type: "post",
                data: requestJson,
                dataType: "json",
                contentType: "application/json;charset=utf-8"
            }).done(function (data) {
                if (data.status == 0) {

                    //请求成功后，写入 access_token
                    layui.data(setter.tableName, {
                        key: setter.request.tokenName
                        , value: data.data.access_token
                    });
                    //将一些管理员的常用信息存储到cookie中
                    saveInfoToCookie(data);
                    //登入成功的提示与跳转
                    layer.msg('登入成功', {
                        offset: '15px'
                        , icon: 1
                        , time: 1000
                    }, function () {
                        location.hash = search.redirect ? decodeURIComponent(search.redirect) : '/';
                    });
                }
                else {
                    layer.msg('密码不对哦！请检查后再输入', {
                        icon: 2
                    });
                }
            }).fail(function (data) {
                layer.alert("请求失败", {icon: 7});
                layer.close(layerIndex);
            });
        });


        /**
         * 将数据存储到Cookie中
         * @param data
         */
        function saveInfoToCookie(data) {
            //将用户名写入cookie中
            $.cookie("name", data.data.name, {
                path: "/",
            });
            //将用户头像路径写入到cookie
            $.cookie("image", data.data.image, {
                path: "/",
            });
            $.cookie("sex", data.data.sex, {
                path: "/",
            });
            $.cookie("description", data.data.description, {
                path: "/",
            });
            $.cookie("id", data.data.id, {
                path: "/",
            });
            $.cookie("time", data.data.time, {
                path: "/",
            });
        }

    });

    function forget() {
        layer.msg('请联系开发者！', {
            offset: '15px'
            , icon: 2
        });
    }
</script>